<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1200px" append-to-body>
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="订单号" prop="order_id">
                    <el-input type="textarea" :rows="4" v-model="queryParams.order_id" placeholder="请输入订单号" clearable style="width: 240px" />
                </el-form-item>
                <el-form-item label="关键字" prop="keyword">
                    <el-input v-model.trim="queryParams.keyword" placeholder="请输入关键字" clearable style="width: 240px" />
                </el-form-item>
                <el-form-item label="SKU" prop="sku">
                    <el-input v-model.trim="queryParams.sku" placeholder="请输入SKU" clearable style="width: 240px" />
                </el-form-item>
                <el-form-item label="渠道标签" prop="tag_source">
                    <el-select v-model="queryParams.tag_source" placeholder="请选择渠道标签" clearable size="small" style="width: 240px">
                        <el-option v-for="item in params.channelLabelList" :key="item.value" :label="item.label" :value="item.value">
                            <span style="float: left">{{ item.label }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="运营标签" prop="tag_campaign">
                    <el-select v-model="queryParams.tag_campaign" placeholder="请选择运营标签" clearable size="small" style="width: 240px">
                        <el-option v-for="item in params.operationLabelList" :key="item.value" :label="item.label" :value="item.value">
                            <span style="float: left">{{ item.label }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="开始时间" prop="createtime">
                    <el-date-picker v-model="queryParams.createtime" format="yyyy-MM-dd" type="daterange"  start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"></el-date-picker>
                </el-form-item>

                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id">
                        <span style="float: left">{{ dict.shop_name }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.type }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>

            </el-form>
        </div>
        <el-table v-loading="loading" :data="tableData" border script row-key="rowKey" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <template v-if="type == 'source'">
                <el-table-column label="渠道" prop="tag_source" min-width="140">
                    <template slot-scope="scope">
                        {{ scope.row.tag_source | formatLabelByValueFuzzy(params.channelLabelList) }}
                    </template>
                </el-table-column>
            </template>
            <template v-else>
                <el-table-column label="SPU" prop="spu" min-width="140"></el-table-column>
            </template>
            <el-table-column label="运营" prop="tag_campaign" min-width="140">
                <template slot-scope="scope">
                    {{ scope.row.tag_campaign | formatLabelByValueFuzzy(params.operationLabelList) }}
                </template>
            </el-table-column>
            <el-table-column label="单量" prop="count" min-width="140"></el-table-column>
            <el-table-column label="销售额" prop="total_price" min-width="140"></el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
    </el-dialog>
</template>

<script>
import {getShopList } from '@/api/shop'
import { querySalesPerformanceChannelList, querySalesPerformanceSkuList } from '@/api/orderSourceWoobilly'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'SourceListDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        // source  sku
        type: {
            type: String,
            default: '',
        },
    },
    data() {
        return {
            // 店铺列表下拉
            shopList: [],
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 查询参数
            queryParams: {
                order_id: undefined,
                sku: undefined,
                tag_source: undefined,
                tag_campaign: undefined,
                createtime: [],
                keyword: undefined,
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
        }
    },
    
    async created() {
        this.loading = true
        try {
            const shopData = await getShopList()
            this.shopList = shopData?.data?.list ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    
    computed: {
        title() {
            return `按${this.type === 'source' ? '渠道' : 'SKU'}查看销售业绩`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val) {
                    this.model = Object.assign({}, this.model, val)
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {
        // this.getList()
    },
    methods: {
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.createtime?.length == 2) {
                qyparams.createtime = [parseTime(qyparams.createtime[0], 'y-m-d'), parseTime(qyparams.createtime[1], 'y-m-d')]
            }
            const request = this.type === 'source' ? querySalesPerformanceChannelList : querySalesPerformanceSkuList
            request(qyparams)
                .then(response => {
                    this.tableData = response.data.map((item, index) => {
                        item.rowKey = `${this.type === 'source' ? item.tag_source : item.spu}--${item.tag_campaign}--${index}`
                        if (item.children) {
                            item.children.map((pre, i) => {
                                pre.rowKey = `${this.type === 'source' ? item.tag_source : item.spu}--${item.count}--${i}`
                            })
                        }
                        return { ...item }
                    })
                    // this.total = response.data?.total ?? 0
                })
                .finally(() => {
                    this.loading = false
                })
        },
        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>

<style>
.order-page {
    min-height: 100%;
    background: #f1f1f1;
}
.order-header {
    margin-bottom: 24px;
    padding-left: 24px;
}
.header-title-title {
    font-size: 18px;
    line-height: 32px;
    font-weight: bold;
    margin-right: 8px;
}
.order-content {
    width: 100%;
    display: flex;
    flex-wrap: nowrap;
}
.order-main {
    width: 66%;
    margin-right: 2%;
}
.order-sub {
    width: 32%;
}
.order-card {
    background: #fff;
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 16px;
    box-shadow: 0 0 4px #eee;
}
.card-content {
    margin: 8px 0;
    border: 1px solid #f5f5f5;
    border-radius: 8px;
}
.card-item {
    padding: 16px;
    border-bottom: 1px solid #f5f5f5;
    display: flex;
    flex-wrap: nowrap;
}
.card-item:last-child {
    border-bottom: 0;
}
.card-item-logo {
    width: 50px;
    height: 50px;
    border: 1px solid #f5f5f5;
    border-radius: 4px;
    margin-right: 16px;
}
.card-item-logo .el-image {
    width: 50px;
    height: 50px;
}
.card-item-content {
    flex: 1;
}
.card-item-left,
.card-item-right {
    width: 15%;
}
.card-item-right {
    text-align: right;
}
.text-bold {
    font-weight: bold;
}
.text-primary {
    color: #11a983;
}
</style>
